package cn.stronglink.asset.terminal.unicom.controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.consumer.entity.Reader003Udev;
import cn.stronglink.asset.model.Alarm;
import cn.stronglink.asset.module.alarm.service.AlarmService;
import cn.stronglink.asset.module.assetmanage.itasset.service.ItassetService;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.rfidState.service.RfidStateService;
import cn.stronglink.asset.terminal.unicom.param.ArrivalVO;
import cn.stronglink.asset.terminal.unicom.param.UpdateAssetParam;
import cn.stronglink.asset.terminal.unicom.service.ApiService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.support.HttpCode;


@Controller
@RequestMapping(value = "api")
public class APIController extends AbstractController {

	@Autowired
	private ItassetService itassetService;
	@Autowired
	private AlarmService alarmService;
	@Autowired
	private ApiService apiService;
	@Autowired
	private RfidStateService rfidStateService;
	

	/**
	 * 3D 接口 查询所有资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "test")
	public ResponseEntity<ModelMap> test(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {	
		List<Reader003Udev> list =new ArrayList<>();
		Reader003Udev aa =new Reader003Udev();
		aa.setRfid("12345678b");
		aa.setU(30);
		list.add(aa);
		try {
			rfidStateService.sendDataToUnicom("101125",list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 3D 接口 查询所有资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "queryRackFor3D")
	public ResponseEntity<ModelMap> queryRackFor3D(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String roomCode) {	
		List<Map<String, Object>> data = itassetService.queryRackFor3D(roomCode);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询所有告警
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "queryCount3D")
	public ResponseEntity<ModelMap> queryCount3D(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String roomCode) {	
		LinkedHashMap<String, Object> data = itassetService.queryCount3D(roomCode);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 3D 接口 查询所有资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "queryRackFor3DTest")
	public ResponseEntity<ModelMap> queryRackFor3DTest(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {	
		  List<Map<String, Object>> rack = new ArrayList<Map<String, Object>>();
		  List<Map<String, Object>> server = new ArrayList<Map<String, Object>>();
		  
		  List<Map<String, Object>> data = itassetService.queryRackFor3DTest();
		  for(Map<String, Object> map:data){
			if(map.get("Type").equals("Rack")){
				rack.add(map);
			}else if(map.get("Type").equals("Server")){
				server.add(map);
			}
          }
		  System.out.println(JSON.toJSON(rack));
		  //System.out.println(JSON.toJSON(server));
		  
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询某个机柜信息   3d 接口(调cmdb的接口)
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param rackCode
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "getRackInfoCmdb")
	public ResponseEntity<ModelMap> getRackInfoCmdb(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String rackCode) throws Exception {	
		String data=null;
		try {
			data = apiService.getRackInfoCmdb(rackCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 查询某个机柜信息   3d 接口
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param rackCode
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "getRackInfo")
	public ResponseEntity<ModelMap> getRackInfo(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String rackCode) throws UnsupportedEncodingException {	
		Map<String, Object> data = itassetService.getRackInfo(rackCode);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 获取服务器信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param serverCode
	 * @return
	 */
	@RequestMapping(value = "getServerInfoCmdb")
	public ResponseEntity<ModelMap> getServerInfoCmdb(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String serverCode) {	
		String data="";
		try {
			data = apiService.getServerInfoCmdb(serverCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 获取服务器信息
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param serverCode
	 * @return
	 */
	@RequestMapping(value = "getServerInfo")
	public ResponseEntity<ModelMap> getServerInfo(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String serverCode) {	
		Map<String, Object> data = itassetService.getServerInfo(serverCode);
		return setSuccessModelMap(modelMap, data);
	}
	
	/**
	 * 手持机同步  上架的资产   并自动创建上架单
	 * @param modelMap
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "syncArrivalList")
	public ResponseEntity<ModelMap> SyncArrivalList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody ArrivalVO arrivalVO) {			
		if(arrivalVO.getId() != null){
			System.out.println(JSON.toJSON(arrivalVO)+"*************同步到货单数据*************");
			String msg =apiService.insertPreset(arrivalVO);
			if(msg!=null){
				return setModelMap(modelMap, HttpCode.CONFLICT, msg);
			}
		}else{
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("CmdbIdIsNull"));
		}
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * cmdb删除资产
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param cmdbId
	 * @return
	 */
	@RequestMapping(value = "delAsset")
	public ResponseEntity<ModelMap> delAsset(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody List<Long> cmdbIds) {	
		System.out.println(JSON.toJSON(cmdbIds)+"删除设备的id****************");
		itassetService.delAsset(cmdbIds);		
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * CMDB的信息修改同步
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param cmdbId
	 * @return
	 */
	@RequestMapping(value = "syncAsset")
	public ResponseEntity<ModelMap> syncAsset(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody ItAssetVo asset) {	
		System.out.println(JSON.toJSON(asset)+"********CMDB的信息修改设备同步********");
		if(asset.getId() !=null){
			String msg = itassetService.updateItAsset(asset);
			if(msg!=null){
				return setModelMap(modelMap, HttpCode.CONFLICT, msg);
			}
		}else{
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("CmdbIdIsNull"));
		}
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 添加报警接口
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@RequestMapping(value = "alarm/in")
	public ResponseEntity<ModelMap> addAlarm(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Alarm alarm) {
		System.out.println(JSON.toJSON(alarm)+"同步告警数据****************");
		if("".equals(alarm.getRack())||"".equals(alarm.getRoom())){
			return setModelMap(modelMap, HttpCode.CONFLICT, "机柜不能为空");
		}
		int count =alarmService.insertAlarm(alarm);
		if(count==3){
			return setModelMap(modelMap, HttpCode.CONFLICT, "该机柜在3D系统中不存在");
		}
		return setSuccessModelMap(modelMap, null);
	}

	
	/**
	 * 取消报警接口
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@RequestMapping(value = "alarm/cancel")
	public ResponseEntity<ModelMap> alarmCancel(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Alarm alarm) {
		System.out.println(JSON.toJSON(alarm)+"取消告警数据****************");
		alarmService.alarmCancel(Long.parseLong(alarm.getAlarmId()));
		return setSuccessModelMap(modelMap, null);
	}
	
	/**
	 * 获取报警接口
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@RequestMapping(value = "alarm/getAlarm")
	public ResponseEntity<ModelMap> getAlarm(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestParam String roomCode) {
		List<Map<String, Object>> alertList = alarmService.getAlarm(roomCode);
		return setSuccessModelMap(modelMap, alertList);
	}
	
	/**
	 * 泰岳下发盘点
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param asset
	 * @return
	 */
	@RequestMapping(value = "inventory")
	public ResponseEntity<ModelMap> inventory(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody List<UpdateAssetParam> rackList) {
		System.out.println(JSON.toJSON(rackList)+"泰岳下发盘点数据****************");
		try {
			apiService.inventoryDiff(rackList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setSuccessModelMap(modelMap, null);
	}


}
